<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
<script type="text/javascript" src="${base}/js/jquery.min.js"></script>
<script src="${base}/js/crytojs/components/core.js"></script>
<script src="${base}/js/crytojs/components/md5.js"></script>
<script src="${base}/js/crytojs/components/evpkdf.js"></script>
<script src="${base}/js/crytojs/components/enc-base64.js"></script>
<script src="${base}/js/crytojs/components/cipher-core.js"></script>
<script src="${base}/js/crytojs/components/aes.js"></script>
<script src="${base}/js/crytojs/components/mode-ecb.js"></script>


<script type="text/javascript">

var aesEncrypt = CryptoJS.AES.encrypt("这是原文abc", "Secret Passphrase");
console.log("简单加密aesEncrypt = %s", aesEncrypt.iv.toString(CryptoJS.enc.Hex));

var aesDecrypt = CryptoJS.AES.decrypt(aesEncrypt, "Secret Passphrase");
console.log("简单解密aesDecrypt = %s", aesDecrypt.toString(CryptoJS.enc.Utf8));
</script>






<script type="text/javascript">


const key = "0102030405060708";  //十六位十六进制数作为密钥
const iv = "0102030405060708";   //十六位十六进制数作为密钥偏移量

//加密方法
function encrypt(word) {
	var keyArray  = CryptoJS.enc.Utf8.parse(key);
	var ivArray  = CryptoJS.enc.Utf8.parse(iv);
	
    var srcs = CryptoJS.enc.Utf8.parse(word);
    
    var encrypted = CryptoJS.AES.encrypt(srcs, keyArray, { iv: ivArray, mode: CryptoJS.mode.CBC, padding: CryptoJS.pad.Pkcs7 });
    return encrypted.ciphertext.toString().toUpperCase();
}

//解密方法
function decrypt(word) {
	var keyArray  = CryptoJS.enc.Utf8.parse(key);
	var ivArray  = CryptoJS.enc.Utf8.parse(iv);
	
    var encodeHexArray = CryptoJS.enc.Hex.parse(word);
    var srcs = CryptoJS.enc.Base64.stringify(encodeHexArray);
    
    var decrypt = CryptoJS.AES.decrypt(srcs, keyArray, { iv: ivArray, mode: CryptoJS.mode.CBC, padding: CryptoJS.pad.Pkcs7 });
    var decryptedStr = decrypt.toString(CryptoJS.enc.Utf8);
    return decryptedStr.toString();
}


function test() {
	var source = $("#source").val();
	var cc = encrypt(source);
	$("#target").val(cc);
}

function test1() {
	var source = $("#sourceS").val();
	var dd = decrypt(source);
	$("#jiemi").val(dd);
}

function decodeStr(){
	var keyword = encrypt("https://www.baidu.com/+=", key);
	window.open("${base}/crytoAes/decode?keyword="+keyword);
}

</script>



<body>
    <h1>F12请看console</h1>
 	<div>
		原文:<textarea id="source" value="" style="width: 500px; height: 90px;" /></textarea>
		<hr>
		密文:<textarea id="target" value="" style="width: 500px; height: 90px;"></textarea>
		<hr>
		<input type="button" onclick="test();" name="" value="加密" />
		<hr>
		密文: <textarea id="sourceS" value="" style="width: 500px; height: 90px;"></textarea>
		<hr>
		原文: <textarea id="jiemi" value="" style="width: 500px; height: 90px;"></textarea>
		<hr>
		<input type="button" onclick="test1();" name="" value="解密" />
		
		<hr>
		<input  type="button"   onclick="decodeStr()"  value="后端解密">
	</div>
</body>
</html>
